package com.wanxi.service.impl;

import com.wanxi.dto.CompanyDTO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.vo.CompanyVO;
import com.wanxi.dao.CompanyDao;
import com.wanxi.dao.impl.CompanyDaoImpl;
import com.wanxi.entity.CompanyEntity;
import com.wanxi.service.CompanyService;
import org.springframework.beans.BeanUtils;
import redis.clients.jedis.Jedis;

import java.util.List;

public class CompanyServiceImpl implements CompanyService {
    private CompanyDao companyDao = new CompanyDaoImpl();

    @Override
    public ResultDTO add(CompanyVO companyVO) {
//        CompanyEntity companyEntity = new CompanyEntity();
//        BeanUtils.copyProperties(companyVO, companyEntity);
        int count = companyDao.add(getCompanyEntity(companyVO));
        if (count > 0) {
            Jedis jedis = new Jedis();
            jedis.del("logo");
        }
        return ResultDTO.getResult(count);
    }

    @Override
    public ResultDTO delete(CompanyVO companyVO) {
//        CompanyEntity companyEntity = new CompanyEntity();
//        BeanUtils.copyProperties(companyVO, companyEntity);
//        CompanyDao companyDao = new CompanyDaoImpl();
//        出来要返回影响的条数count，还要状态码code,还需要具体意思msg
        int count = companyDao.delete(getCompanyEntity(companyVO));
        return ResultDTO.getResult(count > 0 ? "删除成功" : "删除失败", count);
    }

    @Override
    public ResultDTO findById(CompanyVO companyVO) {
//        CompanyEntity companyEntity = new CompanyEntity();
//        BeanUtils.copyProperties(companyVO, companyEntity);
//        CompanyDao companyDao = new CompanyDaoImpl();
        CompanyEntity entity = companyDao.findById(getCompanyEntity(companyVO));
        CompanyDTO companyDTO = new CompanyDTO();
        BeanUtils.copyProperties(entity, companyDTO);
        return ResultDTO.getResult("", 1, companyDTO);
    }

    @Override
    public ResultDTO findAll(CompanyVO companyVO) {

//        CompanyEntity companyEntity = new CompanyEntity();
//        BeanUtils.copyProperties(companyVO, companyEntity);
//        CompanyDao companyDao = new CompanyDaoImpl();
        List<CompanyEntity> companyEntityList = companyDao.findAll(getCompanyEntity(companyVO));
//        List<CompanyDTO> companyDTOList=new ArrayList<>();
//        BeanUtils.copyProperties(entity, companyDTO);
//        System.arraycopy();//数组复制
        return ResultDTO.getResult("", companyEntityList.size(), companyEntityList);
    }

    @Override
    public ResultDTO update(CompanyVO companyVO) {
//        CompanyEntity companyEntity = new CompanyEntity();
//        BeanUtils.copyProperties(companyVO, companyEntity);
//        CompanyDao companyDao = new CompanyDaoImpl();
        int count = companyDao.update(getCompanyEntity(companyVO));
        return ResultDTO.getResult(count);
    }

    private CompanyEntity getCompanyEntity(CompanyVO companyVO) {
        CompanyEntity companyEntity = new CompanyEntity();
        BeanUtils.copyProperties(companyVO, companyEntity);
        return companyEntity;

    }
}
